package com.glela.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.api.upload.qiniu.general.QiniuUtils;
import com.glela.common.util.Base64;
import com.glela.goods.model.request.ImgBasePathRequest;
import com.glela.order.model.request.RequestAfterSalesModel;
import com.glela.order.model.response.ResponseReason;
import com.glela.order.service.AfterSalesService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.util.ParameterUtils;

/**
 * 
 * @author MaoZeYao
 *
 */
@Controller
@RequestMapping("/afterSales")
public class AfterSalesController {
	
	private static Logger logger = LoggerFactory.getLogger(AfterSalesController.class);
	
	@Autowired
	private AfterSalesService afterSalesService;
	
	/**
	 * 售后图片管理
	 * @param request
	 * @return
	 */
	//{"imgBase":"byte64","suffix":"jpg"}
	@RequestMapping(value = "/imgBasePath", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> imgBasePath(@RequestBody ImgBasePathRequest request){
		try {
			if (ParameterUtils.validParams(request.getImgBase())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			if (ParameterUtils.validParams(request.getSuffix())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			
			byte[] b = Base64.decode(request.getImgBase()) ;
			String imgurl = QiniuUtils.uploadByBytys64("file/afterSales/", b, request.getSuffix());
			if ("".equals(imgurl)) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29907), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<String>(imgurl), HttpStatus.OK);
			
		}catch(Exception e) {
			// TODO: handle exception
			logger.error("上传图片异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 发起售后申请
	 * userId orderDetailId
	 * @param model
	 * @author gaoweiqi
	 * @date 2017年8月19日 12:44:05
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/applyAfterSales", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> applyAfterSales(@RequestBody RequestAfterSalesModel model) throws Exception{
		if (ParameterUtils.validParams(model.getUserId(),model.getOrderDetailId())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		return afterSalesService.launchAfterSalesApplication(model);
	}
	
	/**
	 * 发起售后申诉
	 * userId orderDetailId
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/applyComplaint", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> launchComplaintApplication(@RequestBody RequestAfterSalesModel model) throws Exception{
		if (ParameterUtils.validParams(model.getAfterSalesId())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		return afterSalesService.launchComplaintApplication(model.getAfterSalesId());
	}
	
	/**
	 * 提交售后申请
	 * @param model
	 * @author gaoweiqi
	 * @date 2017年8月19日 12:44:05
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/saveAfterSales", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> saveAfterSales(@RequestBody RequestAfterSalesModel model) throws Exception{
		if (ParameterUtils.validParams(model.getUserId(),model.getOrderDetailId(),
			model.getUserRefundReason(),model.getType())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
		}
		return afterSalesService.saveAfterSalesApplication(model);
	}
	
	/**
	 * 保存申诉
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveAfterSaleComplaint", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> saveAfterSaleComplaint(@RequestBody RequestAfterSalesModel model) throws Exception{
		if (ParameterUtils.validParams(model.getAfterSalesId(), model.getType(),
				model.getUserRefundReason(),model.getIsRefund())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
		}
		return afterSalesService.saveAfterSaleComplaint(model);
	}
	
	/**
	 * 查询用户所有的售后信息
	 * @param model  userId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findAfterSalesByUserId", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> findAfterSalesByUserId(@RequestBody RequestAfterSalesModel model) throws Exception{
		if (ParameterUtils.validParams(model.getUserId(),model.getPageNum(),model.getPageSize())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
		}
		return afterSalesService.findAfterSalesByUserId(model);
	}
	
	/**
	 * 查询售后订单详情
	 * @param model afterSalesId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findAfterSalesById", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> findAfterSalesById(@RequestBody RequestAfterSalesModel model) throws Exception{
		if (ParameterUtils.validParams(model.getAfterSalesId())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
		}
		return afterSalesService.findAfterSalesById(model.getAfterSalesId());
	}
	
	/**
	 * 图片凭证更换
	 * @param model   afterSalesId index imgUrl
	 * @return
	 * @throws Exception
	 */
	/*@RequestMapping(value = "/changeImagePool", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> changeImagePool(@RequestBody RequestAfterSalesModel model) throws Exception{
		if (ParameterUtils.validParams(model.getAfterSalesId(),model.getImgIndex())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
		}
		return afterSalesService.changeImagePools(model);
	}*/
	
	/**
	 * 用户填写快递信息（退货）
	 * @param model
	 * @return afterSalesId  courierCompany  courierNumber
	 * @throws Exception
	 */
	@RequestMapping(value = "/addCourierInformation", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> addCourierInformation(@RequestBody RequestAfterSalesModel model) throws Exception{
		if (ParameterUtils.validParams(model.getAfterSalesId(), model.getCourierCompany(), model.getCourierNumber(),
				model.getCourierCompanyEn())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
		}
		return afterSalesService.fillInTheDeliveryInformation(model);
	}
	
	
	/*@RequestMapping(value = "/refundTest", method = RequestMethod.POST, consumes = "application/json")
	public void refundTest(@RequestBody RequestAfterSalesModel model) throws Exception{
		AfterSales afterSale = new AfterSales();
		afterSale.setId(model.getAfterSalesId());
		afterSalesService.userRefundSuccess(afterSale);
	}*/
	
	/**
	 * 拒绝售后
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/storeRefusedAfterSales", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> storeRefusedAfterSales(@RequestBody RequestAfterSalesModel model) throws Exception{
		try {
			if (ParameterUtils.validParams(model.getAfterSalesId(),model.getType())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
			}
			if (model.getType() == 1) {
				if (ParameterUtils.validParams(model.getStoreRefuseReason())) {
					return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
				}
			}
			if (model.getType() == 2) {
				if (ParameterUtils.validParams(model.getStoreRefuseReason1())) {
					return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
				}
			}
			return afterSalesService.storeRefusedAfterSales(model.getAfterSalesId(),model.getStoreRefuseReason(),model.getStoreRefuseReason1());
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("拒绝售后异常：", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 售后商品入库通知系统   !!!!!!!!!!!!!!!!!校妆对接
	 * @param responseReason
	 * @return
	 */
	@RequestMapping(value = "/afterSalesNotice", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> afterSalesNotice(@RequestBody ResponseReason responseReason) {
		try {
			if (ParameterUtils.validParams(responseReason.getAppKey(), responseReason.getData(),
					responseReason.getSign())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return afterSalesService.afterSalesNotice(responseReason.getAppKey(), responseReason.getData(),
					responseReason.getSign());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 拒收订单处理   !!!!!!!!!!!!!!!!!校妆对接
	 * @param responseReason
	 * @return
	 */
	@RequestMapping(value = "/afterSalesRejection", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> afterSalesRejection(@RequestBody ResponseReason responseReason) {
		try {
			if (ParameterUtils.validParams(responseReason.getAppKey(), responseReason.getData(),
					responseReason.getSign())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return afterSalesService.afterSalesRejection(responseReason.getAppKey(), responseReason.getData(),
					responseReason.getSign());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/*@RequestMapping(value = "/afterSalesRejection", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> afterSalesRejection(@RequestBody ResponseReason responseReason) {
		try {
			return afterSalesService.afterSalesRejection(responseReason.getAppKey(), responseReason.getData(),
					responseReason.getSign());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}*/
	
	/**
	 * 手动推送OMS
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/afterNotAuto", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> afterNotAuto(@RequestBody RequestAfterSalesModel model) {
		try {
			afterSalesService.omsReturn(model.getCustomerServiceNumber());
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
}
